using System.Collections;
using System.Collections.Generic;
using System.Linq;
using EventUtils;
using UnityEngine;
using UnityEngine.UI;
using EventType = UnityEngine.EventType;

namespace View.Instant
{
    public class TaskView : View
    {
        private class TaskInfo
        {
            public GameObject _obj;
            public Image _icon;
            public Text _txtCount;
            public Text _txtDesc;
            public Image _imgJindu;
            public Text _txtJindu;
            public Button _btnGet;
            public GameObject _objNo;
            public GameObject _objGetEnd;
            public TaskConfig _taskconfig;
            public PropItemTable _itemTable;
        }
        
        private Button mbtnClose;
        private ScrollRect mScrollrect;
        private Transform mtransParent;
        private GameObject mItem;

        private List<TaskInfo> mTaskInfoList = new List<TaskInfo>();

        public override void OnLoad()
        {
            base.OnLoad();
            
            mbtnClose = transform.Find("btnClose").GetComponent<Button>();
            mScrollrect = transform.Find("ScrollView").GetComponent<ScrollRect>();
            mtransParent = transform.Find("ScrollView/Viewport/Content");
            mItem = transform.Find("Item").gameObject;

            OnInitTaskInfo();
            
            AddClickListener(mbtnClose, () =>
            {
                UIManager.Instance.Close(ViewName.TaskView);
            });
        }

        public void OnInitTaskInfo()
        {
            mTaskInfoList = new List<TaskInfo>();
            var taskinfo = TaskManager.OnGetTaskInfo();
            for (int i = 0; i < taskinfo.Length; i++)
            {
                int index = i;
                TaskInfo taskconfig = new TaskInfo();
                GameObject go = GameObject.Instantiate(mItem);
                var trans = go.transform;
                trans.SetParent(mtransParent);
                trans.localPosition = Vector3.zero;
                trans.localScale = Vector3.one;
                trans.localRotation = Quaternion.identity;
                go.SetActive(true);

                taskconfig._obj = go;
                taskconfig._icon = trans.Find("icon").GetComponent<Image>();
                taskconfig._txtCount = trans.Find("txtcount").GetComponent<Text>();
                taskconfig._txtDesc = trans.Find("desc").GetComponent<Text>();
                taskconfig._imgJindu = trans.Find("jindu").GetComponent<Image>();
                taskconfig._txtJindu = trans.Find("txtjindu").GetComponent<Text>();
                taskconfig._btnGet = trans.Find("btnGet").GetComponent<Button>();
                taskconfig._objNo = trans.Find("btnno").gameObject;
                taskconfig._objGetEnd = trans.Find("btngetend").gameObject;
                taskconfig._taskconfig = taskinfo[i];
                taskconfig._itemTable = GameLocalInfo.OnGetPropItemTableById(taskinfo[i].rewardID);
                mTaskInfoList.Add(taskconfig);
                
                // var goodsitem = ;
                // taskconfig._icon.sprite = ResourceManager.Instance.LoadSprite(goodsitem.path, goodsitem.iconname);
                // taskconfig._txtCount.text = string.Format("x{0}", taskinfo[i].targetNum);
                // taskconfig._txtDesc.text = taskinfo[i].desc;
                // taskconfig._imgJindu.fillAmount = 0;
                // taskconfig._txtJindu.text = "0%";
                // taskconfig._btnGet.gameObject.SetActive(false);
                // taskconfig._objNo.gameObject.SetActive(true);
                // taskconfig._objGetEnd.gameObject.SetActive(false);
                
                AddClickListener(taskconfig._btnGet, () =>
                {
                    if (taskconfig._imgJindu.fillAmount >= 1)
                    {
                        //道具类型1.登录奖励2.观看广告3.累计通关4.累计使用道具
                        GameHelp.OnAddSelfPlayerPropItem(GameLocalInfo.OnGetPropItemTableById(taskinfo[index].rewardID));
                        EventCenter.BroadCast<List<PropItemEnum>>(EventUtils.EventType.EVENT_GONGXIHUODE,new List<PropItemEnum>(){taskconfig._itemTable.enumid});
                        taskconfig._btnGet.gameObject.SetActive(false);
                        taskconfig._objNo.gameObject.SetActive(false);
                        taskconfig._objGetEnd.gameObject.SetActive(true);
                        TaskManager.OnSetTaskCompleteStatusById(taskinfo[index].taskId, -1);
                        TaskManager.OnGetLocalTaskInfo(true);
                        // OnResetItemInfo();
                    }
                });
            }
        }

        public void OnResetItemInfo()
        {
            var taskcompleteInfo = TaskManager.OnGetTaskCompleteDic();
            
            for (int i = 0; i < mTaskInfoList.Count; i++)
            {
                int dickey = mTaskInfoList[i]._taskconfig.taskId;
                if (taskcompleteInfo[dickey][0] <= -1)//任务完成了
                {
                    mTaskInfoList[i]._imgJindu.fillAmount = 1;
                    mTaskInfoList[i]._txtJindu.text = "100%";
                    mTaskInfoList[i]._btnGet.gameObject.SetActive(false);
                    mTaskInfoList[i]._objNo.gameObject.SetActive(false);
                    mTaskInfoList[i]._objGetEnd.gameObject.SetActive(true);
                }
                else
                {
                    float jindu = (taskcompleteInfo[dickey][0] * 1.0f) / taskcompleteInfo[dickey][1];
                    // Debug.LogError("进队："+taskcompleteInfo[dickey][0]+"/"+taskcompleteInfo[dickey][1]+"="+jindu);
                    mTaskInfoList[i]._imgJindu.fillAmount = jindu;
                    mTaskInfoList[i]._txtJindu.text = string.Format("{0}%", (int)(jindu*100));
                    mTaskInfoList[i]._btnGet.gameObject.SetActive(jindu >= 1);
                    mTaskInfoList[i]._objNo.gameObject.SetActive(jindu < 1);
                    mTaskInfoList[i]._objGetEnd.gameObject.SetActive(false);
                }
                
                var goodsitem = GameLocalInfo.OnGetPropItemTableById(mTaskInfoList[i]._taskconfig.rewardID);
                mTaskInfoList[i]._icon.sprite = ResourceManager.Instance.LoadSprite(goodsitem.path, goodsitem.iconname);
                mTaskInfoList[i]._txtCount.text = string.Format("x{0}", mTaskInfoList[i]._taskconfig.targetNum);
                mTaskInfoList[i]._txtDesc.text = mTaskInfoList[i]._taskconfig.desc;
            }
            
            mTaskInfoList.Sort((a, b) => { return a._btnGet.gameObject.activeSelf.CompareTo(b._btnGet.gameObject.activeSelf); });
            
            mScrollrect.verticalNormalizedPosition = 1;
        }
        
        public override void OnShow(params object[] args)
        {
            base.OnShow();

            OnResetItemInfo();
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
        }
    }
}

